<?php

namespace App\Store\Models\Basic;

use App\Common\Services\Common;
use App\Store\Models\Account\SiteStore;
use Illuminate\Database\Eloquent\Model;
use App\Common\Services\Store\Common as StoreCommon;
use Illuminate\Http\Request;

class TemplateTag extends Model
{
    protected $table = 'site_basic_template_tag';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = self::with(['storeinfo'])->where(['cp_id' => $cp_id, 'is_del' => 1]);
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }
        //标签名称
        $tag_name = $request['tag_name'] ?? '';
        if ($tag_name != '') {
            $query->where('tag_name', 'like', '%' . $tag_name . '%');
        }


        $ishavereport=$request['ishavereport'] ?? 0;
        if ($ishavereport==1) {
            $query->where('report_json', '<>','');
        }


        //门店
        $store_id = $request['store_id'] ?? 0;
        if ($store_id > 0) {
            $query->where('store_id', $store_id);
        }

        $query->orderBy('create_at', 'desc');

        $isgetall = $request['isgetall'] ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        }
        $list = $query->get()->toArray();
        return ['list' => $list];
    }
    /**
     *  更新模板默认值
     *
     *
     * */
    public function editDefaultvalue(Request $request)
    {
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $query = TemplateTag::where(['cp_id' => $cp_id, 'is_del' => 1]);
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);

        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }
        $query2 = $query;
        $ids = $query->pluck('id');
        if(!empty($ids)){
            $query->whereIn('id', $ids)->update(['is_default_value'=>1,'update_at'=>time()]);
        }
        $res = $query2->where('id', $request->id)->update(['is_default_value'=>2,'update_at'=>time()]);
        if($res !== false){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 关联门店
     */
    public function storeinfo()
    {
        return $this->hasOne(SiteStore::class, 'id', 'store_id')->select('id','store_name');
    }
}
